package com.tarena.lbs.controller;

import com.tarena.lbs.base.response.JsonResult;
import com.tarena.lbs.pojo.dto.ContentQuery;
import com.tarena.lbs.pojo.dto.ContentSaveParam;
import com.tarena.lbs.pojo.dto.ContentUpdateParam;
import com.tarena.lbs.pojo.vo.ContentVO;
import com.tarena.lbs.service.ContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/content/")
@Api(tags = "文章管理")
public class ContentController {
    @Autowired
    private ContentService contentService;

    @PostMapping("save")
    @ApiOperation("文章保存")
    public JsonResult saveArticle(
            @ApiParam(value = "文章保存参数", required = true)
            @RequestBody ContentSaveParam contentSaveParam
    ){
        log.debug("保存文章参数:{}",contentSaveParam);
        contentService.saveArticle(contentSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("select")
    @ApiOperation("文章列表查询")
    public JsonResult selectContent(
            @ApiParam(value = "文章查询参数", required = false)
            ContentQuery contentQuery
    ){
        log.debug("查询文章参数:{}",contentQuery);
        List<ContentVO> contentList = contentService.selectcontent(contentQuery);
        return JsonResult.ok(contentList);
    }

    @PostMapping("update/{id}")
    @ApiOperation("文章更新")
    public JsonResult updateContent(
            @ApiParam(value = "文章ID", required = true, example = "1")
            @PathVariable Integer id,
            @ApiParam(value = "文章更新参数", required = true)
            @RequestBody ContentUpdateParam contentUpdateParam
    ){
        log.debug("更新文章参数: id={}, params={}", id, contentUpdateParam);
        contentService.updateContent(contentUpdateParam);
        return JsonResult.ok();
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("文章删除")
    public JsonResult delete(
            @ApiParam(value = "文章ID", required = true, example = "1")
            @PathVariable Integer id
    ) {
        log.debug("删除文章参数：{}", id);
        contentService.delete(id);
        return JsonResult.ok();
    }
}